﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DB;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            sceneryDiv.Visible = false;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.Length > 20 ? e.Row.Cells[6].Text.Substring(0, 20) + "..." : e.Row.Cells[6].Text;
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button btn=(Button)sender;
        string sid=btn.CommandArgument;
        DataAccess da = new DataAccess();
        da.RunSql("delete from sceneries where sceneryID=" + sid);
        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('删除成功!')", true);
        GridView1.DataBind();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        SetTxtValue("", "", "", "", "", "", "", "");
        btnNE.Text = "New";
        btnNE.CommandArgument = "N";
        sceneryDiv.Visible = true;
    }

    private void SetTxtValue(string name, string province, string city, string level, string imgurl, string desc, string price, string ccn)
    {
        txtsName.Text = name;
        txtProvice.Text = province;
        txtCity.Text = city;
        txtLevel.Text = level;
        txtImgUrl.Text = imgurl;
        txtDescription.Text = desc;
        txtPrice.Text = price;
        txtCCN.Text = ccn;
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string sid = btn.CommandArgument.ToString();
        hidID.Value = sid;
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader(
            "SELECT [sceneryID],[sceneryName],[province],[city],[level],isnull([imageURL],'') as [imageURL],isnull([description],'') as [description],isnull([price],'') as [price],isnull([creditCardNum],'') as [creditCardNum] FROM [Sceneries] where sceneryID='"
            + sid + "'");
        if (dr.Read())
        {
            SetTxtValue(dr.GetValue(1).ToString(), dr.GetValue(2).ToString(),
                dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), dr.GetValue(6).ToString(), dr.GetValue(7).ToString(), dr.GetValue(8).ToString());
        }
        dr.Close();
        btnNE.Text = "Update";
        btnNE.CommandArgument = "E";
        sceneryDiv.Visible = true;
    }
    protected void btnNE_Click(object sender, EventArgs e)
    {
        DataAccess da = new DataAccess();
        if (btnNE.CommandArgument.ToString() == "N")
        {
            if (CheckInput())
            {
                da.RunSql("insert into sceneries([sceneryName],[province],[city],[level],[imageURL],[description],[price],[creditCardNum])"
                + "values('" + txtsName.Text + "','" + txtProvice.Text + "','" + txtCity.Text + "','" + txtLevel.Text + "','" + txtImgUrl.Text + "','" + txtDescription.Text + "','" + txtPrice.Text + "','" + txtCCN.Text + "')");
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('输入数据格式不正确!')", true);
                return;
            }
        }
        else if (btnNE.CommandArgument == "E")
        {
            if (CheckInput())
            {
                da.RunSql("update sceneries set [sceneryName]='" + txtsName.Text + "',[province]='" + txtProvice.Text + "',[city]='" + txtCity.Text + "',[level]='" + txtLevel.Text + "',[imageURL]='" + txtImgUrl.Text + "',[description]='" + txtDescription.Text + "',[price]='" + txtPrice.Text + "',[creditCardNum]='" + txtCCN.Text + "'");
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('输入数据格式不正确!')", true);
                return;
            }
        }
        SetTxtValue("", "", "", "", "", "", "", "");
        sceneryDiv.Visible = false;
        GridView1.DataBind();
    }

    private bool CheckInput()
    {
        try
        {
            int.Parse(txtLevel.Text);
            float.Parse(txtPrice.Text);
        }
        catch {
            return false;
        }
        return true;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        SetTxtValue("", "", "", "", "", "", "", "");
        sceneryDiv.Visible = false;
    }
}
